<!-- saved from url=(0014)about:internet -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>discreteHSliderScript.as</title>
<link rel="stylesheet" type="text/css" href="../../../../../SourceStyles.css"/>
</head>

<body><pre><span class="ActionScriptReserved">import</span> <span class="ActionScriptDefault_Text">flash</span>.<span class="ActionScriptDefault_Text">events</span>.<span class="ActionScriptDefault_Text">Event</span>;

<span class="ActionScriptReserved">import</span> <span class="ActionScriptDefault_Text">mx</span>.<span class="ActionScriptDefault_Text">controls</span>.<span class="ActionScriptDefault_Text">Alert</span>;
<span class="ActionScriptReserved">import</span> <span class="ActionScriptDefault_Text">mx</span>.<span class="ActionScriptDefault_Text">events</span>.<span class="ActionScriptDefault_Text">SliderEvent</span>;

<span class="ActionScriptReserved">import</span> <span class="ActionScriptDefault_Text">org</span>.<span class="ActionScriptDefault_Text">quantitativesustainability</span>.<span class="ActionScriptDefault_Text">dataStructures</span>.<span class="ActionScriptDefault_Text">AField</span>;
<span class="ActionScriptReserved">import</span> <span class="ActionScriptDefault_Text">org</span>.<span class="ActionScriptDefault_Text">quantitativesustainability</span>.<span class="ActionScriptDefault_Text">events</span>.<span class="ActionScriptDefault_Text">EnableChangeEvent</span>;

<span class="ActionScriptReserved">private</span> <span class="ActionScriptvar">var</span> <span class="ActionScriptDefault_Text">_aField</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">AField</span>;

<span class="ActionScriptReserved">public</span> <span class="ActionScriptfunction">function</span> <span class="ActionScriptDefault_Text">init</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">a</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">AField</span><span class="ActionScriptBracket/Brace">)</span><span class="ActionScriptOperator">:</span><span class="ActionScriptReserved">void</span>
<span class="ActionScriptBracket/Brace">{</span>
    <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">addEventListener</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">SliderEvent</span>.<span class="ActionScriptDefault_Text">CHANGE</span>, <span class="ActionScriptDefault_Text">handleHSlider</span><span class="ActionScriptBracket/Brace">)</span>;
    <span class="ActionScriptDefault_Text">_aField</span><span class="ActionScriptOperator">=</span><span class="ActionScriptDefault_Text">a</span>;

    <span class="ActionScriptComment">// Setup labels:
</span>    <span class="ActionScriptvar">var</span> <span class="ActionScriptDefault_Text">minSlider</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">Number</span><span class="ActionScriptOperator">=</span>1<span class="ActionScriptDefault_Text">e50</span>;
    <span class="ActionScriptvar">var</span> <span class="ActionScriptDefault_Text">maxSlider</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">Number</span><span class="ActionScriptOperator">=-</span>1<span class="ActionScriptDefault_Text">e50</span>;
    <span class="ActionScriptReserved">for</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptvar">var</span> <span class="ActionScriptDefault_Text">j</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">int</span><span class="ActionScriptOperator">=</span>0; <span class="ActionScriptDefault_Text">j</span> <span class="ActionScriptOperator">&lt;</span> <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span>.<span class="ActionScriptDefault_Text">length</span>; <span class="ActionScriptOperator">++</span><span class="ActionScriptDefault_Text">j</span><span class="ActionScriptBracket/Brace">)</span>
    <span class="ActionScriptBracket/Brace">{</span>
        <span class="ActionScriptReserved">if</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">j</span><span class="ActionScriptBracket/Brace">]</span> <span class="ActionScriptOperator">&lt;</span> <span class="ActionScriptDefault_Text">minSlider</span><span class="ActionScriptBracket/Brace">)</span>
            <span class="ActionScriptDefault_Text">minSlider</span><span class="ActionScriptOperator">=</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">j</span><span class="ActionScriptBracket/Brace">]</span>
        <span class="ActionScriptReserved">if</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">j</span><span class="ActionScriptBracket/Brace">]</span> <span class="ActionScriptOperator">&gt;</span> <span class="ActionScriptDefault_Text">maxSlider</span><span class="ActionScriptBracket/Brace">)</span>
            <span class="ActionScriptDefault_Text">maxSlider</span><span class="ActionScriptOperator">=</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">j</span><span class="ActionScriptBracket/Brace">]</span>
    <span class="ActionScriptBracket/Brace">}</span>
    <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">minimum</span><span class="ActionScriptOperator">=</span><span class="ActionScriptDefault_Text">minSlider</span>;
    <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">maximum</span><span class="ActionScriptOperator">=</span><span class="ActionScriptDefault_Text">maxSlider</span>;
    <span class="ActionScriptvar">var</span> <span class="ActionScriptDefault_Text">_labels</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">Array</span><span class="ActionScriptOperator">=</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">minSlider</span>, <span class="ActionScriptDefault_Text">maxSlider</span><span class="ActionScriptBracket/Brace">]</span>;
    <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">labels</span><span class="ActionScriptOperator">=</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">minSlider</span>, <span class="ActionScriptDefault_Text">maxSlider</span><span class="ActionScriptBracket/Brace">]</span>;

    <span class="ActionScriptComment">// Setup caption:
</span>    <span class="ActionScriptDefault_Text">caption</span>.<span class="ActionScriptDefault_Text">text</span><span class="ActionScriptOperator">=</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">label</span>;
    <span class="ActionScriptDefault_Text">captionBox</span>.<span class="ActionScriptDefault_Text">move</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">x</span> <span class="ActionScriptOperator">+</span> <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">width</span> <span class="ActionScriptOperator">/</span> 2.0, <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">y</span> <span class="ActionScriptOperator">-</span> <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">getStyle</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptString">&apos;labelOffset&apos;</span><span class="ActionScriptBracket/Brace">))</span>;
<span class="ActionScriptBracket/Brace">}</span>

<span class="ActionScriptReserved">private</span> <span class="ActionScriptfunction">function</span> <span class="ActionScriptDefault_Text">handleHSlider</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">eventObj</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">Event</span><span class="ActionScriptBracket/Brace">)</span><span class="ActionScriptOperator">:</span><span class="ActionScriptReserved">void</span>
<span class="ActionScriptBracket/Brace">{</span>
    <span class="ActionScriptComment">// find nearest aField
</span>    <span class="ActionScriptReserved">if</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">value</span> <span class="ActionScriptOperator">&gt;=</span> <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">maximum</span><span class="ActionScriptBracket/Brace">)</span>
        <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptOperator">=</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span>.<span class="ActionScriptDefault_Text">length</span>;
    <span class="ActionScriptReserved">else</span> <span class="ActionScriptReserved">if</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">value</span> <span class="ActionScriptOperator">&lt;=</span> <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">minimum</span><span class="ActionScriptBracket/Brace">)</span>
        <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptOperator">=</span>0;
    <span class="ActionScriptReserved">else</span>
    <span class="ActionScriptBracket/Brace">{</span>
        <span class="ActionScriptvar">var</span> <span class="ActionScriptDefault_Text">inc</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">int</span><span class="ActionScriptOperator">=</span>1;
        <span class="ActionScriptReserved">if</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">value</span> <span class="ActionScriptOperator">&lt;</span> <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptBracket/Brace">])</span>
            <span class="ActionScriptDefault_Text">inc</span><span class="ActionScriptOperator">=-</span>1
        <span class="ActionScriptReserved">for</span> <span class="ActionScriptBracket/Brace">(</span>; <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span> <span class="ActionScriptOperator">&lt;</span> <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span>.<span class="ActionScriptDefault_Text">length</span>; <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptOperator">+=</span><span class="ActionScriptDefault_Text">inc</span><span class="ActionScriptBracket/Brace">)</span>
        <span class="ActionScriptBracket/Brace">{</span>
            <span class="ActionScriptReserved">if</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">value</span> <span class="ActionScriptOperator">&gt;=</span> <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptBracket/Brace">]</span> <span class="ActionScriptOperator">&amp;&amp;</span> <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">value</span> <span class="ActionScriptOperator">&lt;</span>
                <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span> <span class="ActionScriptOperator">+</span> 1<span class="ActionScriptBracket/Brace">])</span>
            <span class="ActionScriptBracket/Brace">{</span>
                <span class="ActionScriptReserved">if</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">Math</span>.<span class="ActionScriptDefault_Text">abs</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">value</span> <span class="ActionScriptOperator">-</span> <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptBracket/Brace">])</span> <span class="ActionScriptOperator">&gt;</span> <span class="ActionScriptDefault_Text">Math</span>.
                    <span class="ActionScriptDefault_Text">abs</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">value</span> <span class="ActionScriptOperator">-</span> <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span> <span class="ActionScriptOperator">+</span> 1<span class="ActionScriptBracket/Brace">]))</span>
                    <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptOperator">+=</span>1;
                <span class="ActionScriptReserved">break</span>;
            <span class="ActionScriptBracket/Brace">}</span>
        <span class="ActionScriptBracket/Brace">}</span>
    <span class="ActionScriptBracket/Brace">}</span>
    <span class="ActionScriptReserved">if</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span> <span class="ActionScriptOperator">&gt;=</span> <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span>.<span class="ActionScriptDefault_Text">length</span><span class="ActionScriptBracket/Brace">)</span>
        <span class="ActionScriptDefault_Text">Alert</span>.<span class="ActionScriptDefault_Text">show</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptString">&apos;sdfd&apos;</span><span class="ActionScriptBracket/Brace">)</span>;

    <span class="ActionScriptDefault_Text">moveSlider</span><span class="ActionScriptBracket/Brace">()</span>;
<span class="ActionScriptBracket/Brace">}</span>

<span class="ActionScriptReserved">public</span> <span class="ActionScriptfunction">function</span> <span class="ActionScriptDefault_Text">moveSlider</span><span class="ActionScriptBracket/Brace">()</span><span class="ActionScriptOperator">:</span><span class="ActionScriptReserved">void</span>
<span class="ActionScriptBracket/Brace">{</span>
    <span class="ActionScriptComment">// Move hslider thumb;
</span>    <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">value</span><span class="ActionScriptOperator">=</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptBracket/Brace">]</span>;

    <span class="ActionScriptvar">var</span> <span class="ActionScriptDefault_Text">eventObj</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">EnableChangeEvent</span><span class="ActionScriptOperator">=</span><span class="ActionScriptReserved">new</span> <span class="ActionScriptDefault_Text">EnableChangeEvent</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">EnableChangeEvent</span>.<span class="ActionScriptDefault_Text">ENABLE_CHANGED</span><span class="ActionScriptBracket/Brace">)</span>;
    <span class="ActionScriptDefault_Text">eventObj</span>.<span class="ActionScriptDefault_Text">isEnabled</span><span class="ActionScriptOperator">=</span><span class="ActionScriptReserved">true</span>;
    <span class="ActionScriptDefault_Text">dispatchEvent</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">eventObj</span><span class="ActionScriptBracket/Brace">)</span>;

    <span class="ActionScriptComment">// Move Label
</span>    <span class="ActionScriptvar">var</span> <span class="ActionScriptDefault_Text">y</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">Number</span><span class="ActionScriptOperator">=</span><span class="ActionScriptDefault_Text">hsliderLabel</span>.<span class="ActionScriptDefault_Text">y</span>;
    <span class="ActionScriptvar">var</span> <span class="ActionScriptDefault_Text">x</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">Number</span><span class="ActionScriptOperator">=</span><span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">x</span> <span class="ActionScriptOperator">+</span> <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">width</span> <span class="ActionScriptOperator">*</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">value</span> <span class="ActionScriptOperator">-</span> <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">minimum</span><span class="ActionScriptBracket/Brace">)</span> <span class="ActionScriptOperator">/</span>
        <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">maximum</span> <span class="ActionScriptOperator">-</span> <span class="ActionScriptDefault_Text">hslider</span>.<span class="ActionScriptDefault_Text">minimum</span><span class="ActionScriptBracket/Brace">)</span>;
    <span class="ActionScriptDefault_Text">hsliderLabel</span>.<span class="ActionScriptDefault_Text">move</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">x</span>, <span class="ActionScriptDefault_Text">y</span><span class="ActionScriptBracket/Brace">)</span>;
    <span class="ActionScriptDefault_Text">hsliderLabel</span>.<span class="ActionScriptDefault_Text">htmlText</span><span class="ActionScriptOperator">=</span><span class="ActionScriptString">&apos;&lt;i&gt;&apos;</span> <span class="ActionScriptOperator">+</span> <span class="ActionScriptDefault_Text">String</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span><span class="ActionScriptBracket/Brace">[</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptBracket/Brace">])</span> <span class="ActionScriptOperator">+</span> <span class="ActionScriptString">&apos;&lt;/i&gt;&apos;</span>;
<span class="ActionScriptBracket/Brace">}</span>

<span class="ActionScriptReserved">public</span> <span class="ActionScriptfunction">function</span> <span class="ActionScriptReserved">set</span> <span class="ActionScriptDefault_Text">incAIndex</span><span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">i</span><span class="ActionScriptOperator">:</span><span class="ActionScriptDefault_Text">int</span><span class="ActionScriptBracket/Brace">)</span><span class="ActionScriptOperator">:</span><span class="ActionScriptReserved">void</span>
<span class="ActionScriptBracket/Brace">{</span>
    <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptOperator">+=</span><span class="ActionScriptDefault_Text">i</span>;
    <span class="ActionScriptReserved">if</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span> <span class="ActionScriptOperator">&lt;</span> 0<span class="ActionScriptBracket/Brace">)</span> <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptOperator">=</span>0;
    <span class="ActionScriptReserved">if</span> <span class="ActionScriptBracket/Brace">(</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span> <span class="ActionScriptOperator">&gt;=</span> <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span>.<span class="ActionScriptDefault_Text">length</span><span class="ActionScriptBracket/Brace">)</span> <span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">index</span><span class="ActionScriptOperator">=</span><span class="ActionScriptDefault_Text">_aField</span>.<span class="ActionScriptDefault_Text">array</span>.<span class="ActionScriptDefault_Text">length</span><span class="ActionScriptOperator">-</span>1;

    <span class="ActionScriptDefault_Text">moveSlider</span><span class="ActionScriptBracket/Brace">()</span>;
<span class="ActionScriptBracket/Brace">}</span>

</pre></body>
</html>
